package ar.com.ctrln.view.dashboard.compras.insumo;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.EventQueue;
import org.zkoss.zk.ui.event.EventQueues;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.VariableResolver;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zk.ui.select.annotation.WireVariable;
import org.zkoss.zul.Footer;
import org.zkoss.zul.Grid;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

import ar.com.ctrln.control.mantenimiento.insumo.ConsultarInsumo;
import ar.com.ctrln.persistence.model.insumo.Insumo;
import ar.com.ctrln.view.ui.comportamientoVentana.AbstractMinimizarVentana;

@VariableResolver(org.zkoss.zkplus.spring.DelegatingVariableResolver.class)
public class ConsultarInsumoController extends AbstractMinimizarVentana{

	private static final long serialVersionUID = 4448027582905493675L;

	@WireVariable
	private ConsultarInsumo consultarInsumo;
	
	@Wire
	private Window wConsultarInsumo;
	
	@Wire
	private Grid insumosGrid;

	@Wire
	private Footer footer_category;
	
	@Wire
	private Footer footer_msg;
	
	@Wire("#txtFilter")
	private Textbox txtFilter;
	
	private List<Insumo> insumos;

	private EventQueue<Event> eq;

	
	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		eq = EventQueues.lookup("refreshInsumos", EventQueues.DESKTOP, true);
        eq.subscribe(new EventListener<Event>() {
            public void onEvent(Event event) throws Exception {
            	insumosGrid.setModel(new ListModelList<Insumo>(consultarInsumo.listaInsumo()));
            	
            }
        });
        insumosGrid.setModel(getConsultaInsumo());
	}
	
	public ListModel<Insumo> getConsultaInsumo() {
		insumos = consultarInsumo.listaInsumo();
		return new ListModelList<Insumo>(insumos);
	}
		

	void imprimirTickets(){
		//Executions.createComponents("/zul/reportes/listadoTicket.zul",null, null);
	}
	
	/////////////////////////////FUNCION FILTRO////////////////////////////
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Listen("onChanging = #txtFilter")
	public void getFilter(Event event) {
		((Textbox) event.getTarget())
				.setValue(((org.zkoss.zk.ui.event.InputEvent) event).getValue());
		insumosGrid.setModel(new ListModelList(getFilterBy(txtFilter.getValue(),insumos)));
		if (insumosGrid.getRows().getChildren().size() > 0) {
			footer_msg.setLabel("");
			footer_category
					.setLabel("Un total de "
							+ insumosGrid.getRows().getChildren().size()
							+ " Insumos");
		} else { 
			footer_msg.setLabel("Ningun Resultado...");
			footer_category.setLabel("");
		}
		((Textbox) event.getTarget()).setFocus(true);
	}
	
	public static List<Insumo> getFilterBy(String condicion, Collection<Insumo> insumos) {
		List<Insumo> data = new ArrayList<Insumo>();
		for (Iterator<Insumo> i = insumos.iterator(); i.hasNext();) {
			Insumo tmp = i.next();
			if (tmp.getTipoInsumo().toLowerCase()
					.indexOf(condicion.trim().toLowerCase()) >= 0
					|| tmp.getNombre().toLowerCase()
							.indexOf(condicion.trim().toLowerCase()) >= 0) {
				data.add(tmp);
			}
		}
		return data;
	}
	/////////////////////////////FUNCION FILTRO////////////////////////////

	@Listen("onClick = #btnCancelarConsulta")
	public void cerraVentana(Event event) {
		wConsultarInsumo.onClose();
	}
	
	
	
}